<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="nodeList"
      row-key="nodeId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="nodeId" label="节点编号"></el-table-column>
      <el-table-column prop="nodeName" label="节点名称"></el-table-column>
      <el-table-column prop="fileSize" label="大小"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="downloadLog(scope.row)"
            v-if="scope.row.isDirectory == 'N'"
          >下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {getRunLogList} from "@/api/system/runLog";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "RunLog",
  dicts: [],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      nodeList: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {},
      //下载参数
      downLoadParams: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询节点列表 */
    getList() {
      this.loading = true;
      getRunLogList(this.queryParams).then(response => {
        this.nodeList = this.handleTree(response.data, "nodeId", "parentNodeId", "children");
        this.loading = false;
      });
    },
    downloadLog(row) {
      this.downLoadParams.downLoadPath = row.downLoadPath;
      this.downLoadParams.nodeName = row.nodeName;
      this.download('system/run/log/download', {
        ...this.downLoadParams
      }, row.nodeName)
    },
    /** 转换节点数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.nodeId,
        label: node.nodeName,
        children: node.children
      };
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
  }
};
</script>
